Skip to main content

Sending ETH with Axir Smart Account

This guide explains how to send ETH using your Axir smart account.

Prerequisites

First, import the required dependencies:

import { AxirCore } from "axr-erc4337-sdk";
import { ethers } from "ethers";
import { type Hex } from "viem";

Implementation

1. Initialize AxirCore

First, create an instance of AxirCore:

const axirCore = new AxirCore(
process.env.PRIVATE_KEY as Hex,
process.env.RPC_URL as string,
process.env.BUNDLER_URL as string,
BigInt(0), // nonce
"baseSepolia" // network name
);

2. Send ETH Transaction

Follow these steps to send an ETH transaction:

// Prepare ETH transfer UserOperation
const userOp = await axirCore.prepareUserOperation(
{
contract: receiverAddress,
value: ethers.parseEther("0.01"),
abi: [], // Empty for ETH transfers
functionName: "",
args: [],
},
undefined,
usePaymaster,
paymasterType
);

// Estimate gas costs
const gasEstimate = await axirCore.estimateUserOperationGas(
userOp,
usePaymaster,
paymasterType
);

// Log gas estimates
console.log("Gas Estimation for ETH transfer:");
if (gasEstimate.totalGasFeeInEth) {
console.log(
`Estimated gas fee in ETH: ${ethers.formatEther(
gasEstimate.totalGasFeeInEth
)} ETH`
);
}
if (gasEstimate.totalGasFeeInToken) {
console.log(
`Estimated gas fee in tokens: ${ethers.formatUnits(
gasEstimate.totalGasFeeInToken,
gasEstimate.tokenDecimals
)} ${gasEstimate.tokenSymbol}`
);
}

// Execute the transaction
const txHash = await axirCore.executeUserOperation(userOp);
console.log("Transaction hash:", txHash);
info

The gas estimation step is optional but recommended to give users an idea of transaction costs before execution.

tip

You can use different paymaster types by changing the usePaymaster and paymasterType parameters. Available paymaster types are:

  • 'ETHPaymaster'
  • 'ERC20Paymaster'
  • 'AXRPaymaster'
  • 'AXRERC20Paymaster'